<?php

namespace App\Model;

use Illuminate\Http\Request;

class GenerationChargeRecord extends BaseModel
{
    protected $connection = 'mysql';
    protected $table = 'generation_charge_record';
    protected $fillable = ['user_id','uuid','nickname','charge_price','charge_type','charge_desc','admin_id','enter_item_status','enter_item_desc','created_at'];
    private $fields = ['id','rule_id','uuid','nickname','charge_price','charge_type','charge_desc','admin_id','enter_item_status','enter_item_desc','enter_item_img','created_at'];

    public function getChargePriceAttribute($value){
        return $value/100;
    }

    public function getList(Request $request){
        $limit = $request->input('limit',10);
        $page = $request->input('page',1);
        $uuid = $request->input('uuid','');
        $nickname = $request->input('nickname','');
        $beginDate = $request->input('begin_date',0);
        $endDate = $request->input('end_date',0);
        $admin_id = $request->input('admin_id',0);
        $enter_item_status = $request->input('enter_item_status');

        $query = new self();

        if($uuid) $query = $query->where('uuid',$uuid);
        if($nickname) $query = $query->where('nickname','like',"%{$nickname}%");
        if($admin_id) $query = $query->where('admin_id',$admin_id);
        if(isset($enter_item_status)) $query = $query->where('enter_item_status',$enter_item_status);
        if($beginDate && $endDate) $query = $query->whereBetween('created_at',[$beginDate,$endDate]);
        if($beginDate && !$endDate) $query = $query->where('created_at','>=',$beginDate);
        if(!$beginDate && $endDate) $query = $query->where('created_at','<=',$endDate);

        return $query->with(['admin:id,name'])->orderBy('id','desc')->paginate($limit,$this->fields,'page',$page);
    }

    public function createData(Request $request, $data){
        return self::create(array_merge($request->all(), $data));
    }

    public function enterItem(Request $request, int $id){
        $model = self::find($id);
        $model->enter_item_status = $request->input('enter_item_status');
        $model->enter_item_desc = $request->input('enter_item_desc');
        $model->enter_item_img = $request->input('enter_item_img');
        return $model->save();
    }

    public function updateData(Request $request,$id){
        $model = self::find($id);
        $model->enter_item_status = 1;
        $model->enter_item_desc = $request->input('enter_item_desc');
        $model->enter_item_img = $request->input('enter_item_img');
        return $model->save();
    }

    public function admin(){
        return $this->belongsTo(Users::class,'admin_id','id');
    }

    public function statisticsData(){
        $model = new self();
        return $model->get(['charge_price','enter_item_status','charge_type'])->groupBy('charge_type')->toArray();
    }

    /**批量插入充值记录
     * @param $data
     * @return mixed
     */
    public function batchInsertData($data){
        foreach ($data as $d){
            write_log('addLog', 'data', $data);
            self::create($d);
        }
    }
}
